// pagesMine/pages/clientInfo/clientInfo.js
import ajax from "../../../utils/index"
import dayjs from '../../../utils/dayjs.min'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    records: [],
    reachBottom: true,
  },

  /**
    * 获取直推详情
    */
  get_custom_info: function () {
    let that = this
    ajax.get('/wx/appletSpread/info', {
      userId: that.data.userId
    }).then(res => {
      if (res.code == 200) {
        let info = res.data
        info.joinTeamTime = dayjs(info.joinTeamTime).format("YYYY-MM-DD HH:mm")
        info.lastLoginTime = dayjs(info.joinTeamTime).format("YYYY-MM-DD HH:mm")
        info.registerTime = dayjs(info.joinTeamTime).format("YYYY-MM-DD HH:mm")
        that.setData({ info })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 获取日志
   */
  get_user_records: function () {
    let that = this
    ajax.get('/wx/appletUserLog/list',{
      userId: that.data.userId,
      pageSize: 10,
      pageNum: that.data.pageNum
    }).then(res => {
      if (res.code == 200) {
        that.setData({
          records: [...that.data.records, ...res.rows]
        })
        if (res.total <= that.data.pageNum * 10) {
          that.setData({
            reachBottom: false
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 联系 - 拨打电话
   */
  contact: function () {
    if (!this.data.info.phone) return ajax.showToast('没有手机号码')
    wx.makePhoneCall({
      phoneNumber: this.data.info.phone,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.id
    })
    this.get_custom_info()
    this.get_user_records()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.reachBottom) {
      let pageNum = this.data.pageNum
      pageNum++
      this.setData({
        pageNum: pageNum
      })
      this.get_user_records()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})